﻿using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using JinJun.YShow.Romote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinJun.YShow.BLL.Service
{
    public class UserInfoService:BaseService<UserInfo>,IUserInfoService
    {
        public List<AddressAdmin> GetAdminUserList(string username, string startdate, string enddate)
        {


            //创建List对象           
            //创建实例            
            using (var db = new YShowDBEntities())
            {
                var result = from a in db.UserInfo
                             join b in db.User_Address
                             on a.Id equals b.UserId
                             select new AddressAdmin()
                             {
                                 Id = a.Id,
                                 UserName = a.UserName,
                                 RealName = a.RealName,
                                 UserSex = a.UserSex,
                                 Phone = a.Phone,
                                 CreateTime = a.CreateTime,
                                 Status = a.Status,
                                 Province = b.Province,
                                 City = b.City,
                                 District = b.District,
                                 Address = b.Address
                             };
                if (startdate != null && enddate != null && startdate != string.Empty && enddate != string.Empty)
                {
                    DateTime kaishi = Convert.ToDateTime(startdate);
                    DateTime jiesu = Convert.ToDateTime(enddate);

                    return result.Where(b => b.CreateTime > kaishi && b.CreateTime < jiesu).ToList();
                }
                else if (username != null)
                {
                    return result.Where(b => b.UserName == username).ToList();
                }
                else
                {
                    return result.ToList();
                }
            }
        }


        public bool Deleteuser(UserInfo user)
        {
            using (var db = new YShowDBEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {

                        //添加第一张表
                        db.UserInfo.Attach(user);
                        db.UserInfo.Remove(user);
                        //保存
                        db.SaveChanges();
                        //添加第二张表
                        User_Address address = new User_Address();

                        address = db.User_Address.SingleOrDefault(a => a.UserId == user.Id);
                        db.User_Address.Attach(address);
                        db.User_Address.Remove(address);

                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        //throw ex.InnerException;
                        return false;
                    }
                }

            }
            return true;
        }


        public AdminUser Vologin(AdminUser adminUser)
        {
            HttpGetApi getApi = new HttpGetApi();
            return getApi.ValidateLogin(adminUser);

        }


        public List<Menu> GetMenuList(string modulename)
        {
            MenaApi menaApi = new MenaApi();
            return menaApi.Getmenuapi(modulename);
        }
    }
}
